package com.baomidou.ant.controller;

import com.baomidou.ant.common.Result;
import com.baomidou.ant.entity.Notice;
import com.baomidou.ant.service.INoticeService;
import com.baomidou.ant.utils.VoUtilsTool;
import com.baomidou.ant.vo.Request.RequestDeleteVo;
import com.baomidou.ant.vo.Request.RequestSaveNoticeVo;
import com.baomidou.ant.vo.Request.RequestUpdateNoticeVo;
import com.baomidou.ant.vo.Response.ResponseNoticeVo;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private INoticeService noticeService;

    //上传公告插图,通过公告id上传
    @RequestMapping("/upload/{noticeId}")
    public Result upload(@RequestParam("file") MultipartFile file, HttpServletRequest request, @PathVariable Integer noticeId) {
        String path = noticeService.upload(file);
        //保存图片路径
        HttpSession session=request.getSession();
        session.setAttribute("path",path);
        Notice notice =noticeService.getById(noticeId);
        notice.setNoticeImage((String)session.getAttribute("path"));
        noticeService.saveOrUpdate(notice);
        return Result.getSuccess().setMsg("上传成功");
    }

    //用户展示notice
    @PostMapping("/listOfUser")
    public Result listOfUser(){
        List<Notice> noticeList=noticeService.listOfUser();
        String s=String.valueOf(noticeList.size());
        return Result.getSuccess().setData(noticeList).setMsg(s);
    }

    //新增notice
    @PostMapping("/save")
    public Result save(@RequestBody RequestSaveNoticeVo requestSaveNoticeVo){
        //判断
        if (VoUtilsTool.checkObjFieldIsNull(requestSaveNoticeVo)){
            return Result.getFailure().setMsg("不能有空项");
        }
        Notice notice=new Notice();
        BeanUtils.copyProperties(requestSaveNoticeVo,notice);
        if(noticeService.save(notice)){
            return Result.getSuccess().setMsg("添加成功");
        }else{
            return Result.getFailure().setMsg("添加失败");
        }
    }


    //修改notice
    @PostMapping("/update")
    public Result update(@RequestBody RequestUpdateNoticeVo requestUpdateNoticeVo){
        Notice notice = noticeService.getById(requestUpdateNoticeVo.getId());
        //判断空项，保留原值
        if(ObjectUtils.isEmpty(requestUpdateNoticeVo.getEndTime())){
            requestUpdateNoticeVo.setEndTime(notice.getEndTime());
        }
        if(ObjectUtils.isEmpty(requestUpdateNoticeVo.getNoticeText())){
            requestUpdateNoticeVo.setNoticeText(notice.getNoticeText());
        }
        if(ObjectUtils.isEmpty(requestUpdateNoticeVo.getTitle())){
            requestUpdateNoticeVo.setTitle(notice.getTitle());
        }
        //修改信息并保存
        BeanUtils.copyProperties(requestUpdateNoticeVo,notice);
        if (noticeService.updateById(notice)) {
            return Result.getSuccess().setMsg("修改成功");
        } else {
            return Result.getSuccess().setMsg("修改失败");
        }
    }

    //通过id删除
    @DeleteMapping("/delete/{noticeId}")
    public Result delete(@PathVariable Integer noticeId){
        if(noticeService.removeById(noticeId)){
            return Result.getSuccess().setMsg("删除成功!");
        }else {
            return Result.getFailure().setMsg("删除失败!");
        }
    }

    //批量
    @DeleteMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody RequestDeleteVo requestDeleteVo){
        List<Integer> ids= Arrays.asList(requestDeleteVo.getIds());
        if(ids.size()>0&&noticeService.removeByIds(ids)){
            return Result.getSuccess().setMsg("删除成功");
        }else{
            return Result.getFailure().setMsg("请勾选要删除的项！");
        }
    }

}
